package system

import (
	consts2 "MoSkeleton/config/consts"
	"MoSkeleton/framework/core/consts"
	"MoSkeleton/framework/core/dto/response"
	"MoSkeleton/framework/utils"
	"MoSkeleton/global"
	"MoSkeleton/services"
	"MoSkeleton/web/pogos/request/system"
	"MoSkeleton/web/v1/http/base"
	"fmt"
	"github.com/gin-gonic/gin"
	"go.uber.org/zap"
	"strconv"
	"time"
)

type SysDictDataCtrl struct {
	base.BaseCtrl
}

func (d *SysDictDataCtrl) GetCtrlTitle() string {
	msg, _ := utils.Translater.Translate(consts.TKey_TitleSysDictDataMsg)
	return msg
}

// GetDataListByDictType 根据字典类型获取字典数据
// @Tags 数据字典数据
// @Summary 根据字典类型获取字典数据
// @Security ApiKeyAuth
// @Param dictType query string true "字典类型"
// @Success 200 {object} response.Response{data=[]system.SysDictDataItemResp} "{"code": 200, "data": [...]}"
// @Router /system/dict/data/type/{dictType} [get]
func (d *SysDictDataCtrl) GetDataListByDictType(c *gin.Context) {
	dictType := c.Param("dictType")
	if dictType == "" {
		global.MoLogger.Info("参数错误")
		response.FailMsgAsI18n(c, consts.ValidatorParamsCheckFailCode, consts.TKey_ValidatorParamsCheckFailMsg)
		return
	}
	dataItemsResp, err := services.ServiceGroupInst.SystemGroupInst.SysDictDataServiceInst.GetDataListByDictType(dictType)
	if err != nil {
		global.MoLogger.Info("查询数据字典失败", zap.Error(err))
		response.FailMsgAsI18n(c, consts.CommSelectFailCode, consts.TKey_DictDataGetFailMsg)
		return
	}
	response.SuccessAsI18n(c, consts.TKey_CommGetSuccessMsg, dataItemsResp)
}

// GetDictDataById 根据id获取字典数据
// @Tags 数据字典数据
// @Summary 根据id获取字典数据
// @Security ApiKeyAuth
// @Param id path string true "字典数据id"
// @Success 200 {object} response.Response{data=system.SysDictDataItemResp} "{"code": 200, "data": {...}}"
// @Router /system/dict/data/{id} [get]
func (d *SysDictDataCtrl) GetDictDataById(c *gin.Context) {
	idStr := c.Param("id")
	id, err := strconv.ParseUint(idStr, 10, 64)
	if err != nil {
		global.MoLogger.Info("参数错误", zap.Error(err))
		response.FailMsgAsI18n(c, consts.ValidatorParamsCheckFailCode, consts.TKey_ValidatorParamsCheckFailMsg)
		return
	}
	dataItemResp, err := services.ServiceGroupInst.SystemGroupInst.SysDictDataServiceInst.GetDictDataById(id)
	if err != nil {
		global.MoLogger.Info("查询数据字典失败", zap.Error(err))
		response.FailMsgAsI18n(c, consts.CommSelectFailCode, consts.TKey_DictDataGetFailMsg)
		return
	}
	response.SuccessAsI18n(c, consts.TKey_CommGetSuccessMsg, dataItemResp)
}

// GetDictDataPage 分页获取字典数据
// @Tags 数据字典数据
// @Summary 分页获取字典数据
// @Security ApiKeyAuth
// @Param dictDataPageReq query system.DictDataPageReq true "分页查询参数"
// @Success 200 {object} response.Response{data=response.PageResult} "{"code": 200, "data": [...]}"
// @Router /system/dict/data/list [get]
func (d *SysDictDataCtrl) GetDictDataPage(c *gin.Context) {
	dictDataPageReq := system.DictDataPageReq{}
	if err := c.ShouldBindQuery(&dictDataPageReq); err != nil {
		global.MoLogger.Info("参数错误", zap.Error(err))
		response.FailMsgAsI18n(c, consts.ValidatorParamsCheckFailCode, consts.TKey_ValidatorParamsCheckFailMsg)
		return
	}
	dictDataPageReq.ShouldBindParams(c)
	pageResult, err := services.ServiceGroupInst.SystemGroupInst.SysDictDataServiceInst.GetDictDataPage(&dictDataPageReq)
	if err != nil {
		global.MoLogger.Info("查询数据字典失败", zap.Error(err))
		response.FailMsgAsI18n(c, consts.CommSelectFailCode, consts.TKey_DictDataGetFailMsg)
		return
	}
	response.SuccessAsI18n(c, consts.TKey_CommGetSuccessMsg, pageResult)
}

// ExportDictData 导出字典数据
// @Tags 数据字典数据
// @Summary 导出字典数据
// @Security ApiKeyAuth
// @Param dictDataPageReq query system.DictDataPageReq true "分页查询参数"
// @Success 200 {object} response.Response{msg=string} "{"code": 200, "data": [...]}"
// @Router /system/dict/data/export [get]
func (d *SysDictDataCtrl) ExportDictData(c *gin.Context) {
	d.IgnoreLogResponseData(c)
	dictDataPageReq := system.DictDataPageReq{}
	if err := c.ShouldBind(&dictDataPageReq); err != nil {
		global.MoLogger.Info("参数错误", zap.Error(err))
		response.FailMsgAsI18n(c, consts.ValidatorParamsCheckFailCode, consts.TKey_ValidatorParamsCheckFailMsg)
		return
	}
	if buf, err := services.ServiceGroupInst.SystemGroupInst.SysDictDataServiceInst.ExportDictData(&dictDataPageReq); err != nil {
		global.MoLogger.Info("导出数据字典失败", zap.Error(err))
		response.FailMsgAsI18n(c, consts.ServerOccurredErrorCode, consts.TKey_DictDataExportFailMsg)
		return
	} else {
		response.SuccessWithFileData(c, fmt.Sprintf(consts2.Excel_Export_DictData, time.Now().Unix()), buf)
	}
}

// AddDictData 添加字典数据
// @Tags 数据字典数据
// @Summary 添加字典数据
// @Security ApiKeyAuth
// @Param dictDataAddReq body system.DictDataAddReq true "添加字典数据参数"
// @Success 200 {object} response.Response{msg=string} "{"code": 200, "data": {...}}"
// @Router /system/dict/data [post]
func (d *SysDictDataCtrl) AddDictData(c *gin.Context) {
	dictDataAddReq := system.DictDataAddReq{}
	if err := c.ShouldBindJSON(&dictDataAddReq); err != nil {
		global.MoLogger.Info("参数错误", zap.Error(err))
		response.FailMsgAsI18n(c, consts.ValidatorParamsCheckFailCode, consts.TKey_ValidatorParamsCheckFailMsg)
		return
	}
	dictDataAddReq.CreatedBy = utils.GetUsernameFromGin(c)
	dictDataAddReq.UpdatedBy = dictDataAddReq.CreatedBy
	if ok, err := services.ServiceGroupInst.SystemGroupInst.SysDictDataServiceInst.AddDictData(&dictDataAddReq); err != nil {
		global.MoLogger.Info("添加数据字典失败", zap.Error(err))
		response.FailMsgAsI18n(c, consts.CommCreatFailCode, consts.TKey_DictDataAddFailMsg)
		return
	} else if !ok {
		response.FailMsgAsI18n(c, consts.CommCreatFailCode, consts.TKey_CommCreatFailMsg)
		return
	}
	response.SuccessAsI18n(c, consts.TKey_CommCreatSuccessMsg, nil)
}

// UpdateDictDataById 更新字典数据
// @Tags 数据字典数据
// @Summary 更新字典数据
// @Security ApiKeyAuth
// @Param dictDataUpdateReq body system.SysDictDataUpdateReq true "更新字典数据参数"
// @Success 200 {object} response.Response{msg=string} "{"code": 200, "data": {...}}"
// @Router /system/dict/data [put]
func (d *SysDictDataCtrl) UpdateDictDataById(c *gin.Context) {
	updateReq := system.SysDictDataUpdateReq{}
	if err := c.ShouldBindJSON(&updateReq); err != nil {
		global.MoLogger.Info("参数错误", zap.Error(err))
		response.FailMsgAsI18n(c, consts.ValidatorParamsCheckFailCode, consts.TKey_ValidatorParamsCheckFailMsg)
		return
	}
	updateReq.UpdatedBy = utils.GetUsernameFromGin(c)
	if ok, err := services.ServiceGroupInst.SystemGroupInst.SysDictDataServiceInst.UpdateDateTypeById(&updateReq); err != nil {
		global.MoLogger.Info("更新数据字典失败", zap.Error(err))
		response.FailMsgAsI18n(c, consts.CommUpdateFailCode, consts.TKey_DictDataUpdateFailMsg)
		return
	} else if !ok {
		response.FailMsgAsI18n(c, consts.CommUpdateFailCode, consts.TKey_CommUpdateFailMsg)
		return
	}
	response.SuccessAsI18n(c, consts.TKey_CommUpdateSuccessMsg, nil)
}

// DeleteById 根据id删除字典数据
// @Tags 数据字典数据
// @Summary 根据id删除字典数据
// @Security ApiKeyAuth
// @Param id path int true "字典数据id"
// @Success 200 {object} response.Response{msg=string} "{"code": 200, "data": {...}}"
// @Router /system/dict/data/{id} [delete]
func (d *SysDictDataCtrl) DeleteById(c *gin.Context) {
	idStr := c.Param("id")
	id, err := strconv.ParseUint(idStr, 10, 64)
	if err != nil {
		global.MoLogger.Info("参数错误", zap.Error(err))
		response.FailMsgAsI18n(c, consts.ValidatorParamsCheckFailCode, consts.TKey_ValidatorParamsCheckFailMsg)
		return
	}
	if ok, err := services.ServiceGroupInst.SystemGroupInst.SysDictDataServiceInst.DeleteById(id); err != nil {
		global.MoLogger.Info("删除数据字典失败", zap.Error(err))
		response.FailMsgAsI18n(c, consts.CommDeleteFailCode, consts.TKey_DictDataDeleteFailMsg)
		return
	} else if !ok {
		response.FailMsgAsI18n(c, consts.CommDeleteFailCode, consts.TKey_CommDeleteFailMsg)
		return
	}
	response.SuccessAsI18n(c, consts.TKey_CommDeleteSuccessMsg, nil)
}
